package com.mindainfo.groundwave.server.member.dao.impl;

import com.mindainfo.groundwave.server.base.dao.impl.BaseDaoImpl;
import com.mindainfo.groundwave.server.member.bean.SysMember;
import com.mindainfo.groundwave.server.member.bean.SysPermission;
import com.mindainfo.groundwave.server.member.bean.SysRole;
import com.mindainfo.groundwave.server.member.dao.IMemberDao;
import org.mongodb.morphia.query.FindOptions;
import org.mongodb.morphia.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by MD-01 on 2017/5/15.
 */
@Repository
public class MemberDaoImpl extends BaseDaoImpl<SysMember> implements IMemberDao {
    /**
     * 根据用户名查找
     * @param username
     * @return
     */
    public SysMember queryByUsername(String username){
        return getDatastore().createQuery(SysMember.class)
                    .field("username").equal(username)
                    .get();
    }

    @Override
    public List<SysMember> queryList(int start, int num, String key,List<Integer> deptIds) {
        Query<SysMember> query = getDatastore().createQuery(SysMember.class);
        if(!StringUtils.isEmpty(key)){
            query.or(
                    query.criteria("username").contains(key),
                    query.criteria("realName").contains(key)
                    );
        }
        query.field("deptId").in(deptIds);
        FindOptions options = new FindOptions();
        options.skip(start).limit(num);
        return query.order("-createTime").asList(options);
    }

    @Override
    public int queryListCount(String key,List<Integer> deptIds) {
        Query<SysMember> query = getDatastore().createQuery(SysMember.class);
        if(!StringUtils.isEmpty(key)){
            query.or(
                    query.criteria("username").contains(key),
                    query.criteria("realName").contains(key)
            );
        }
        query.field("deptId").in(deptIds);
        return (int) query.count();
    }
}
